package com.desmart.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.desmart.util.PropertiesUtil;
import com.dingxianginc.ctu.client.CaptchaClient;
import com.dingxianginc.ctu.client.model.CaptchaResponse;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class Login
 */
@WebServlet("/login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/text");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String token = request.getParameter("token");
		Map<String, String> result = new HashMap<String, String>();
		try {
			if(username != null && !"".equals(username)) {
				if(password != null && !"".equals(password)) {
					if(password.equals("123456")) {
						/**构造入参为appId和appSecret
						 * appId和前端验证码的appId保持一致，appId可公开
						 * appSecret为秘钥，请勿公开
						 * token在前端完成验证后可以获取到，随业务请求发送到后台，token有效期为两分钟**/
						String appId = PropertiesUtil.getProperty("appId");
						String appSecret = PropertiesUtil.getProperty("appSecret");
						CaptchaClient captchaClient = new CaptchaClient(appId,appSecret);
						//captchaClient.setCaptchaUrl(captchaUrl); 
						//特殊情况需要额外指定服务器,可以在这个指定，默认情况下不需要设置
						CaptchaResponse captchaResponse = captchaClient.verifyToken(token);
						System.out.println(token);
						System.out.println(captchaResponse.getCaptchaStatus());
						//确保验证状态是SERVER_SUCCESS，SDK中有容错机制，在网络出现异常的情况会返回通过
						if (captchaResponse.getResult()) {
						    /**token验证通过，继续其他流程**/
							result.put("status", "0");
							result.put("msg", "验证通过");
						} else {
						    /**token验证失败，业务系统可以直接阻断该次请求或者继续弹验证码**/
							result.put("status", "1");
							result.put("msg", "验证码错误");
						}
					} else {
						result.put("status", "1");
						result.put("msg", "密码错误");
					}
				} else {
					result.put("status", "1");
					result.put("msg", "请输入密码");
				}
			} else {
				result.put("status", "1");
				result.put("msg", "请输入用户名");
			}
			out.println(JSONObject.fromObject(result));
			out.flush();
			out.close();
			return;
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
